package com.huaao.micropolice.root.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huaao.micropolice.core.model.system.Button;
import com.huaao.micropolice.core.model.system.Menu;
import com.huaao.micropolice.core.service.system.ButtonService;
import com.huaao.micropolice.core.service.system.MenuService;

@Controller
@RequestMapping("")
public class IndexController {
	
	private static final Logger log = Logger.getLogger(IndexController.class);
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private ButtonService buttonService;
	
	@RequestMapping("index.do")
	public String index(){
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			List<Menu> menus = menuService.findRoot();
			request.setAttribute("menus", menus);
			log.info("首页");
			return "index";
		}else{
			return "login";
		}
	}
	
	@RequestMapping("roleMappingDept.do")
	public String roleMappingDept(String roleId){
		request.setAttribute("roleId", roleId);
		return "/system/role/mappingDept";
	}
	
	@RequestMapping("btnAdd.do")
	public String btnAdd(){
		return "/system/button/edit";
	}
	
	@RequestMapping("showSingleCheckMenuTree.do")
	public String showSingleCheckMenuTree(String menuId){
		return "/public/menu/showSingleCheckMenuTree";
	}
	
	@RequestMapping("btnLoad.do")
	public String btnLoad(String id){
		Button btn = buttonService.load(id);
		request.setAttribute("btn", btn);
		return "/system/button/edit";
	}
	
	@RequestMapping("roleMenu.do")
	public String roleMenu(String id){
		request.setAttribute("id", id);
		return "/system/role/roleMenu";
	}
}
